using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcExtExample.Models;
using Newtonsoft.Json;

namespace MvcExtExample.Handlers
{
    /// <summary>
    /// Summary description for LogHistoryOperation
    /// </summary>
    public class WellOperation : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string act = context.Request.Params["act"] ?? "";
            string output = "";
            switch (act.ToLower())
            {
                case "add":
                    output = Add(context.Request);
                    break;
                case "del":
                    output = Del(context.Request);
                    break;
                case "edit":
                    output = Edit(context.Request);
                    break;
                case "read":
                    output = Read();
                    break;
                default:
                    output = "{'success':false,'msg':'错误的操作类型'}";
                    break;
            }
            context.Response.ContentType = "text/javascript";
            context.Response.Write(output);
        }

        private string Read()
        {
            throw new NotImplementedException();
        }

        private string Edit(HttpRequest httpRequest)
        {
            string data = httpRequest.Params["data"] ?? "";
            List<CommonWell> logs = new List<CommonWell>();
            if (data.Length > 0)
            {
                logs = ProcessData(data);
                try
                {
                    foreach (CommonWell item in logs)
                    {
                        string sql = "UPDATE [dbo].[CommonWell] SET [Well_Type_ID]='" + item.Well_Type_ID + "',Dep_ID='" + item.Dep_ID + "',[Status]='" + item.Status + "' WHERE [Well_ID]='" + item.Well_ID + "'";
                        framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
                        cs.RunSQL(sql);
                    }
                    return "{'success':true}";
                }
                catch (Exception e)
                {
                    return "{'success':false,'Msg':'" + e.Message + "'}";
                }
            }
            else
            {
                return "{'success':false,'Msg':'错误的提交数据！'}";
            }
        }

        private string Add(HttpRequest httpRequest)
        {
            string data = httpRequest.Params["data"] ?? "";
            List<CommonWell> CommonWell = new List<CommonWell>();
            if (data.Length > 0)
            {
                CommonWell = ProcessData(data);
                try
                {
                    foreach (CommonWell item in CommonWell)
                    {
                        string sql = "INSERT INTO [dbo].[CommonWell] ([Well_ID],[Well_Type_ID],[Dep_ID],[Status]) VALUES('"+item.Well_ID+"','"+item.Well_Type_ID+"','"+item.Dep_ID+"',N'"+item.Status+"')";
                        framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
                        cs.RunSQL(sql);
                    }
                    return "{'success':true}";
                }
                catch (Exception e)
                {
                    return "{'success':false,'Msg':'" + e.Message + "'}";
                }
            }
            else
            {
                return "{'success':false,'Msg':'错误的提交数据！'}";
            }
        }

        private string Del(HttpRequest httpRequest)
        {
            string data = httpRequest.Params["data"] ?? "";
            List<CommonWell> logs = new List<CommonWell>();
            if (data.Length > 0)
            {
                logs = ProcessData(data);
                try
                {
                    foreach (CommonWell item in logs)
                    {
                        string sql = "DELETE FROM [dbo].[CommonWell] WHERE [Well_ID]='" + item.Well_ID + "'";
                        framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
                        cs.RunSQL(sql);
                    }
                    return "{'success':true}";
                }
                catch (Exception e)
                {
                    return "{'success':false,'Msg':'" + e.Message + "'}";
                }
            }
            else
            {
                return "{'success':false,'Msg':'错误的提交数据！'}";
            }
        }

        private List<CommonWell> ProcessData(string data)
        {
            List<CommonWell> logs = new List<CommonWell>();
            logs = JsonConvert.DeserializeObject<List<CommonWell>>(data);
            return logs;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}